package artifact

import (
	"bytes"
	"strconv"
	"ys.calc/pointFunction"
	"ys.calc/role"
)

type SubStatWord struct {
	SubGroupStr string
	StatWord    [7]StatWords
}

func initSubStatWord(group SubGroup) SubStatWord {
	atk := getSubAtk(group.Atk)
	defense := getSubDefense(group.Defense)
	hp := getSubHp(group.Hp)
	mastery := getSubElementalMastery(group.Mastery)
	recharge := getSubRecharge(group.Recharge)
	rate := getSubCritRate(group.Rate)
	dmg := getSubCritDmg(group.Dmg)
	word := [7]StatWords{
		atk,
		defense,
		hp,
		mastery,
		recharge,
		rate,
		dmg,
	}

	subGroupStr := generateSubGroupStr(group)

	return SubStatWord{subGroupStr, word}
}

func generateSubGroupStr(group SubGroup) string {
	var buffer bytes.Buffer
	atk := int(group.Atk)
	if atk > 0 {
		buffer.WriteString(strconv.Itoa(atk))
		buffer.WriteString("Atk ")
	}
	defense := int(group.Defense)
	if defense > 0 {
		buffer.WriteString(strconv.Itoa(defense))
		buffer.WriteString("Defense ")
	}
	hp := int(group.Hp)
	if hp > 0 {
		buffer.WriteString(strconv.Itoa(hp))
		buffer.WriteString("Hp ")
	}

	mastery := int(group.Mastery)
	if mastery > 0 {
		buffer.WriteString(strconv.Itoa(mastery))
		buffer.WriteString("Mastery ")
	}

	recharge := int(group.Recharge)
	if recharge > 0 {
		buffer.WriteString(strconv.Itoa(recharge))
		buffer.WriteString("Recharge ")
	}

	rate := int(group.Rate)
	buffer.WriteString(strconv.Itoa(rate))
	buffer.WriteString("Rate ")
	buffer.WriteString(strconv.Itoa(int(group.Dmg)))
	buffer.WriteString("Dmg")

	subConfigStr := buffer.String()
	return subConfigStr
}

func getSubAtk(times float64) StatWords {
	return StatWords{
		wordValue: 5,
		BonusFunc: func(r *role.Role) {
			pointFunction.PercentATK(5*times, r)
		},
	}
}

func getSubDefense(times float64) StatWords {
	return StatWords{
		wordValue: 5.2,
		BonusFunc: func(r *role.Role) {
			pointFunction.PercentDefense(5.2*times, r)
		},
	}
}

func getSubElementalMastery(times float64) StatWords {
	return StatWords{
		wordValue: 20,
		BonusFunc: func(r *role.Role) {
			pointFunction.AddElementalMastery(20*times, r)
		},
	}
}

func getSubHp(times float64) StatWords {
	return StatWords{
		wordValue: 5,
		BonusFunc: func(r *role.Role) {
			pointFunction.PercentHP(5*times, r)
		},
	}
}
func getSubRecharge(times float64) StatWords {
	return StatWords{
		wordValue: 5.5,
		BonusFunc: func(r *role.Role) {
			pointFunction.AddRecharge(5.5*times, r)
		},
	}
}

func getSubCritRate(times float64) StatWords {
	return StatWords{
		wordValue: 3.3,
		BonusFunc: func(r *role.Role) {
			pointFunction.AddCritRate(3.3*times, r)
		},
	}
}

func getSubCritDmg(times float64) StatWords {
	return StatWords{
		wordValue: 6.6,
		BonusFunc: func(r *role.Role) {
			pointFunction.AddCritDmg(6.6*times, r)
		},
	}
}
